import request from "/utils/request"
import { getMerchantList } from "/api/clock/index"
const Promise = require('/plugins/es6-promise')

Component({
  mixins: [],
  data: {
    status: 1,
    pageSize: 10,
    search: "",
    private: {
      total: 0,
      list: []
    },
    signed: {
      total: 0,
      list: []
    },
    currrentClickIndex: null,
    currentShopList: []
  },
  props: {
    onChange: () => { }
  },
  didMount() {
    this.queryData({
      status: 1,
      search: this.data.search,
      pageSize: this.data.pageSize,
      pageNumber: 1
    }).then(() => {
      this.queryData({
        status: 2,
        search: this.data.search,
        pageNumber: 1,
        pageSize: this.data.pageSize
      })
    })
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    // 页面被拉到底部
    onReachBottom() {
      if (this.data.status === 1 && this.data.private.total !== this.data.private.list.length) {
        this.queryData({
          status: this.data.status,
          search: this.data.search,
          pageSize: this.data.pageSize,
          pageNumber: this.data.private.list.length / this.data.pageSize + 1
        })
      } else if (this.data.status === 2 && this.data.signed.total !== this.data.signed.list.length) {
        this.queryData({
          status: this.data.status,
          search: this.data.search,
          pageSize: this.data.pageSize,
          pageNumber: this.data.signed.list.length / this.data.pageSize + 1
        })
      }
    },
    onSearch(e) {
      this.setData({
        search: e.detail.value
      })
      this.queryData({
        status: 1,
        search: this.data.search,
        pageSize: this.data.pageSize,
        pageNumber: 1
      }).then(() => {
        this.queryData({
          status: 2,
          search: this.data.search,
          pageSize: this.data.pageSize,
          pageNumber: 1
        })
      })
    },
    onTabSelect(e) {
      this.setData({
        status: e.target.dataset.status
      })
    },
    onMerchantSelect(e) {
      this.setData({
        currrentClickIndex: e.target.dataset.index
      })
      const key = this.data.status === 1 ? 'private' : 'signed'
      const shopList = this.data[key].list[this.data.currrentClickIndex].shopList
      this.setData({
        currentShopList: shopList
      })
    },
    onShopPickerClose() {
      this.setData({
        currrentClickIndex: null,
        currentShopList: []
      })
    },
    bindPickerChange() {

    },
    onShopPickerChange(shop) {
      const key = this.data.status === 1 ? "private" : "signed"
      const merchant = JSON.parse(JSON.stringify(this.data[key].list[this.data.currrentClickIndex]))
      delete merchant.shopList
      merchant.shop = shop
      this.props.onChange(merchant)
    },
    queryData(params) {
      const key = params.status === 1 ? "private" : "signed"
      if (params.pageNumber === 1) {
        let obj = {}
        obj[key] = {
          total: 0,
          list: []
        }
        this.setData(obj)
      }
      return new Promise((resolve) => {
        dd.showLoading({
          content: "加载中..."
        })
        request({
          url: getMerchantList,
          data: params
        }).then(res => {
          res.voList.map(item => {
            if (item.tags) {
              item.tags = item.tags.split('&&')
            }
            item.shopList.map(val => {
              val.shopName = `${val.shopName}${val.branchName ? '(' + val.branchName + ')' : ''}`
            })
          })
          let obj = {}
          if (params.pageNumber === 1) {
            obj[key] = {
              total: res.totalNum,
              list: res.voList
            }
            this.setData(obj)
          } else {
            this.data[key].list.push(...res.voList)
            obj[key] = this.data[key]
            this.setData(obj)
          }
        }).finally(() => {
          resolve()
          dd.hideLoading();
        })
      })
    }
  },
});
